<?php
/*
+--------------------------------------------------------------------------
|   phpBIZ v3.0  full version
|   ========================================
|   by taft@wjl.cn yejun@wjl.cn
|   http://www.phpbiz.cn http://www.wjl.cn
|   all rights reserved
+---------------------------------------------------------------------------
|
|   > 最后修改日期：2005-4-30 2006-2-8
|
+--------------------------------------------------------------------------
*/
!class_exists('Debug') && exit('Forbidden');
$s = new Sellercp2;
class Sellercp2
{
	var $seller_data;
	var $upload_dir;

	function Sellercp2()
	{
		global $BIZ,$INCOME,$CONF;
		
		$this -> upload_dir = stripslashes($CONF['upload_dir']);
		$this -> seller_data = new manager($BIZ->seller['seller_id']);

		//---------------------------------------
		// Kill globals - globals bad, Homer good.
		//---------------------------------------
		
		$tmp_in = array_merge( $_GET, $_POST, $_COOKIE );
		
		foreach ( $tmp_in as $k => $v )	unset($$k);
		
	    //--------------------------------------------
        // Check viewing permissions, etc
	    //--------------------------------------------
						
		if ( empty($BIZ->seller['seller_id']) or $BIZ->seller['seller_id']=="" or $BIZ->seller['seller_id']==0 )
		{
			$BIZ->error( "用户没有登陆，不能使用此功能","?act=Login&code=00" );
		}
	    if( $BIZ->seller['seller_check_status']=='uncheck' )
		{
			$this -> control_panel("nocharge");
		}elseif( ($data=$this -> seller_data->get_seller_data("end_time,property_id"))&& $data['end_time']<time()&&$_LEVEL[$data['property_id']][1]>0 )
		{
			$this -> control_panel("due");
		}else
		{
			$BIZ -> assign('code',$INCOME['code']);
			$BIZ-> nav_link[] =array ("商家控制台","index.php?act=Seller");
			switch($INCOME['code'])
			{
				case '00':     // display general seller info
				   $this -> control_panel();
				   break;
				case '01':
					$BIZ-> nav_link[] = array("分类查找商品","");
					$this -> search_form();
				    break;
				case '02':
					$BIZ-> nav_link[] = array("分类查找商品","");
					$this -> search_goods();
					break;
				case 'addg':
					$BIZ-> nav_link[] = array("快速上架","");
					$this -> add_goods();   // directly
                    break;
				case 'doaddg':
					$this -> do_add_goods();
				    break;
				default:
				  $this -> control_panel();
			}
		}
		$BIZ -> output('Seller2.tpl');
	}

	function control_panel($msg='')    
	{  // 00 , default
		global $BIZ,$DB;
         
		//------------------------------
		//fill display stuff here
        //------------------------------
        if($msg=='nocharge')
		     $BIZ ->assign("notice","您目前还<font class=blue size=+2>未付费</font>，所有功能暂时停止。请点击<a href='?act=User&code=buy'>[<font class=red>这里</font>]</a>付费开通。");
		elseif($msg=='due')
			$BIZ ->assign("notice","您目前购买等级<font class=blue size=+2>已到期</font>，所有功能暂时停止。请点击<a href='?act=User&code=buy'>[<font class=red>这里</font>]</a>续费开通。");

		$DB -> db_query("SELECT count(*) as ct FROM biz_goods WHERE seller_id = ".$BIZ ->seller['seller_id']." AND goods_mark_sell='0' UNION ALL SELECT count(*) as ct FROM biz_shop WHERE seller_id = ".$BIZ ->seller['seller_id']);
		$r_g = $DB -> db_fetch_row();
		$BIZ -> assign("gnum",$r_g['ct']);

		$r_s = $DB -> db_fetch_row();
		$BIZ -> assign("snum",$r_s['ct']);

		$DB -> db_query("SELECT count(*) as ct from biz_message  JOIN biz_goods ON seller_id = ".$BIZ ->seller['seller_id']."  AND message_owner_goods_id = goods_id AND message_reply_content IS NULL");
        $r_m = $DB -> db_fetch_row();
		$BIZ -> assign("mnum",$r_m['ct']);
		//----------------------------------
		//fill recent user property info
		//----------------------------------
		$BIZ -> assign("prop",$this -> seller_data -> build_property_info());
		return;
	}
	
	function _list_shop()
	{
		global $BIZ,$DB;

		$DB -> db_query("SELECT * FROM biz_shop WHERE seller_id ='".$BIZ->seller['seller_id']."'" );
		if( !$DB -> db_fetch_num() )  
		{
			$BIZ -> assign("none",1);
		}
		while( $r = $DB -> db_fetch_row() )
		{
			$sn[] = $r['shop_name'];
			$sid[] = $r['shop_id'];
		}

		return array($sn,$sid);
	}

	function search_form()
	{
		global $BIZ;
        $r = $this-> _list_shop();
		$BIZ -> assign("shop_name",$r[0]);
		$BIZ -> assign("shop_id",$r[1]);
		return;
	}
	function search_goods()
	{
		global $BIZ,$DB,$INCOME;
		
		$gid = intval($INCOME['gid']);

		$mark = intval($INCOME['mark']);
		$active =intval($INCOME['active']);
		if($mark==0 || $mark==1) $mark_str = " AND goods_mark_sell=".$mark;
		if( $active==0 ) $active_str = " AND g.goods_end_active_time >".time();
		if( $active==1 ) $active_str = " AND g.goods_end_active_time <".time();
		// find goods which have message 
		$DB -> db_query( "SELECT g.*, count(m.message_content) as ct FROM biz_goods g JOIN biz_message m ON m.message_owner_goods_id = g.goods_id AND m.message_reply_content IS NULL WHERE seller_id = ".$BIZ ->seller['seller_id'].$mark_str.$active_str." AND g.shop_id =".$gid." GROUP BY g.goods_id  ");

		while( $g_re = $DB -> db_fetch_row() )
		{
			$g_data[] = array( $g_re['goods_name'],date( "y-m-d H:i",$g_re['goods_store_date']),date("y-m-d H:i",$g_re['goods_end_active_time']),$g_re['goods_price']."(".$g_re['goods_discount'].")",$g_re['goods_visit_count'],$g_re['goods_mark_sell'],$g_re['goods_id'],$g_re['ct'] );
		}
		
		//find goods which not have message
        $DB -> db_query("( SELECT g.* FROM biz_goods g left JOIN biz_message m ON m.message_owner_goods_id = g.goods_id  WHERE g.seller_id = ".$BIZ ->seller['seller_id']." AND g.shop_id =".$gid.$mark_str.$active_str." AND m.message_content IS NULL ) UNION  (SELECT g.* FROM biz_goods g left JOIN biz_message m ON m.message_owner_goods_id = g.goods_id  WHERE g.seller_id = ".$BIZ ->seller['seller_id']." AND g.shop_id =".$gid.$mark_str.$active_str." AND m.message_content IS NOT NULL AND m.message_reply_content IS NOT NULL) ");
		
		while( $g_re = $DB -> db_fetch_row() )
		{
			$g_data[] = array( $g_re['goods_name'],date( "y-m-d H:i",$g_re['goods_store_date']),date("y-m-d H:i",$g_re['goods_end_active_time']),$g_re['goods_price']."(".$g_re['goods_discount'].")",$g_re['goods_visit_count'],$g_re['goods_mark_sell'],$g_re['goods_id'],$g_re['ct'] );
		}
		
		if( !count($g_data) ) 
		$BIZ-> assign("msg","提示：您还未登陆商品或还未有该类商品。请开新店后添加或在原店里增加商品");	
		//$BIZ -> error("","");
		$BIZ -> assign( "g_data", $g_data );

	}

	function add_goods()
	{
		global $BIZ,$DB;
		
		$res = $DB -> db_query( "SELECT * FROM biz_shop WHERE seller_id ='".$BIZ->seller['seller_id']."'" );
        
		if( !$DB -> db_fetch_num() )  
		{
			$BIZ -> assign("none",1);
		}
		//check 
        $this -> seller_data ->load_property();
		if( !$this -> seller_data ->check_property("goodst") )
			$BIZ -> error("您已经出售了允许的最大商品数量。如想登陆更多商品请<a href='index.php?act=User&code=upgrade'>[<font class=red>升级</font>]</a>");
		
		$r = $this-> _list_shop();
		$BIZ -> assign("shop_name",$r[0]);
    	$BIZ -> assign("shop_id",$r[1]);

		//-----------------------------------------------------
		//  read the cat info data
		//-----------------------------------------------------

		$DB -> db_query( " SELECT a.goods_cat_name as aname,a.goods_cat_id as aid,b.goods_cat_name as bname,b.goods_cat_id as bid FROM biz_goods_cat a LEFT JOIN biz_goods_cat b ON b.goods_cat_parent_id =a.goods_cat_id WHERE a.goods_cat_parent_id = 0" );
        
		$c_r = $DB -> db_fetch_row();
		$pre_id = $c_r['aid'];
		$cat_id[] = $c_r['aid'];
		$cat_name[] = $c_r['aname'];
		if($c_r['bid'])
		{
			$cat_id[] = $c_r['bid'];
		    $cat_name[] = "&nbsp;&nbsp;&nbsp;|-".$c_r['bname'];
		}
		while( $c_r = $DB -> db_fetch_row() )
		{
			if( $pre_id != $c_r['aid'] ) 
			{
				$cat_id[] = $c_r['aid'];
				$cat_name[] = $c_r['aname'];
				$pre_id =  $c_r['aid'];
			}
			if($c_r['bid'])
			{
				$cat_id[] = $c_r['bid'];
				$cat_name[] = "&nbsp;&nbsp;&nbsp;|-".$c_r['bname'];

			}
		}
		$BIZ -> assign("cat_id",$cat_id);
		$BIZ -> assign("cat_name",$cat_name);
		return;
	}

	function do_add_goods()
	{
		global $BIZ,$INCOME,$DB;

		//-------------------------------------------------------
		//first do the upload file stuff if necessary
        //-------------------------------------------------------
	    $pic_id = 1;
		if($_FILES['up_pic']['name'])	
		{    
			 $prefix='bip_g'.time().mt_rand(1,99999);   // 'g' stands for goods
			 $f_upload = new upload_file($prefix,$this -> upload_dir); 
             $f_upload->set_file_name_size($_FILES['up_pic']['name'],$_FILES['up_pic']['size']); 
             $f_upload->set_upfile($_FILES['up_pic']['tmp_name']);  
			 $file_name = $f_upload->do_save();
		
		     $DB -> db_query( "INSERT INTO `biz_pic`"."(`pic_name` ".") VALUES ( '{$file_name}')");
		     $pic_id = $DB -> db_insert_id();
		}
		
		//-------------------------------------------------------
		$ag = $INCOME['ag'];
		$ag['seller_id'] = $BIZ -> seller['seller_id'];  
		$ag['shop_id'] = intval($INCOME['shop_id']);
		$ag['goods_store_date'] = time();
		$to_day = $this -> seller_data -> get_valid_day();    //计算商品失效时间
		$ag['goods_end_active_time'] = time()+ $to_day*3600*24;
		$ag['goods_prime_picture_id'] = $pic_id;
		$DB -> db_form_insert("biz_goods",$ag);
        $goods_id = $DB -> db_insert_id();
		if($pic_id!=1) $DB -> db_query( " UPDATE biz_pic SET pic_goods_id ='".$goods_id."' WHERE pic_id='".$pic_id."'" );

		$this -> _update_seller_data("gincrease");
		$BIZ -> update_repute('idx_goods_add');
   	    $BIZ ->redirect_screen("恭喜！您已经成功上架了一件商品","?act=Seller&code=16");
	}
	
	function _update_seller_data($action,$row=1)
	{
		global $DB,$BIZ;
		switch($action)
		{
			case 'sincrease':
				$DB -> db_query("UPDATE biz_seller_data SET shop_total = shop_total + $row WHERE seller_id =".$BIZ->seller['seller_id']);
				break;
			case 'sdecrease':
				$DB -> db_query("UPDATE biz_seller_data SET shop_total = shop_total - $row WHERE seller_id =".$BIZ->seller['seller_id']);
				break;
			case 'gincrease':
				$DB -> db_query("UPDATE biz_seller_data SET goods_total = goods_total + $row WHERE seller_id=".$BIZ->seller['seller_id']);
				break;
			case 'gdecrease':
				$DB -> db_query("UPDATE biz_seller_data SET goods_total = goods_total - $row WHERE seller_id=".$BIZ->seller['seller_id']);
				break;
		}
	}
}
?>
